package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.PageResult;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.dto.AdvposDto;
import com.itheima.ydd.entity.Advimg;
import com.itheima.ydd.entity.Advpos;
import com.itheima.ydd.service.AdvimgService;
import com.itheima.ydd.service.AdvposService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/ydd_advpos")
public class AdvposController {

    @Autowired
    private AdvposService advposService;
    @Autowired
    private AdvimgService advimgServier;

    /**
     * 分页查询
     *
     * @param page
     * @param limit
     * @param advposdesc
     * @param advposname
     * @return
     */
    @GetMapping("/list")
    public ResultData list(Long page, Long limit, String advposdesc, String advposname) {
        log.info("分页查询对象信息：{}", page + limit + advposdesc + advposname);
        //创建分页构造器
        Page<Advpos> pageInfo = new Page<>(page, limit);
        LambdaQueryWrapper<Advpos> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(Advpos::getAdvposdesc, advposdesc);
        queryWrapper.like(Advpos::getAdvposname, advposname);

        advposService.page(pageInfo, queryWrapper);
        //总条数
        Long count = Long.valueOf(advposService.count(queryWrapper));
        //创建分页返回对象，并将插叙拿结果拷贝到分页对象中
        Page<AdvposDto> pageResult = new Page<>();
        BeanUtils.copyProperties(pageInfo, pageResult, "records");
        List<Advpos> advposList = pageInfo.getRecords();
        List<AdvposDto> list = advposList.stream().map((item) -> {
            AdvposDto advposDto = new AdvposDto();
            BeanUtils.copyProperties(item, advposDto);
            Integer id = item.getId();
            LambdaQueryWrapper<Advimg> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(Advimg::getAdvimgpos, id);
            List<Advimg> list1 = advimgServier.list(wrapper);
            if (list1 != null) {
                int num = list1.size();
                advposDto.setAdvposnum(num);
            }
            return advposDto;
        }).collect(Collectors.toList());

        return ResultData.ok("", PageResult.init(count, limit, page, list));
    }

    /**
     * 修改
     *
     * @param advpos
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody Advpos advpos) {
        //构造查询器
        LambdaQueryWrapper<Advpos> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Advpos::getId, advpos.getId());

        Advpos advposServiceOne = advposService.getOne(queryWrapper);
        advposServiceOne.setAdvposname(advpos.getAdvposname());
        advposServiceOne.setAdvposdesc(advpos.getAdvposdesc());
        advposServiceOne.setAdvpossize(advpos.getAdvpossize());
        advposService.update(advposServiceOne, queryWrapper);

        return ResultData.ok("修改成功", null);
    }

    /**
     * 根据id查询广告详情
     *
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData rowInfo(Long id) {

        LambdaQueryWrapper<Advpos> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Advpos::getId, id);
        Advpos one = advposService.getOne(queryWrapper);
        return ResultData.ok("", one);
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData delete(Long id) {
        LambdaUpdateWrapper<Advpos> queryWrapper = new LambdaUpdateWrapper<>();
        //     LambdaQueryWrapper<Advpos> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Advpos::getId, id);
        queryWrapper.set(Advpos::getDefaultData, "0");
        advposService.update(queryWrapper);
        return ResultData.ok("删除成功", null);

    }


    /**
     * 查询全部
     *
     * @return
     */
    @GetMapping("/all")
    public ResultData all() {
        List<Advpos> list = advposService.list();
        return ResultData.ok("", list);
    }

    /**
     * 添加
     *
     * @param advpos
     * @return
     */
    @PostMapping("/add")
    public ResultData add(@RequestBody Advpos advpos) {
        advpos.setDefaultData("1");
        advposService.save(advpos);
        return ResultData.ok("添加成功", null);
    }
}
